package com.example.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.controller.vo.Result;
import com.example.entity.Category;
import com.example.entity.Interaction;
import com.example.entity.PageView;
import com.example.service.ICategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 商品类别表 前端控制器
 * </p>
 *
 * @author yzy
 * @since 2025-08-04
 */
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private ICategoryService iCategoryService;

    @GetMapping("/list")
    public Result list(@RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                       @RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize) {
        //创建分页对象
        Page<Category> page = new Page<>(pageNum, pageSize);

        //执行分页查询
        Page<Category> page1 = iCategoryService.page(page);
        //返回结果
        PageView pageView = new PageView();
        pageView.setPageSize(Integer.parseInt(page1.getSize() + ""));
        pageView.setPageNum(Integer.parseInt(page1.getCurrent() + ""));
        pageView.setAllRow(page1.getTotal());
        pageView.setTotalPage(page1.getPages());
        pageView.setData(page1.getRecords());
        return Result.success(pageView);
    }


    @PutMapping("/edit")
    public Result add(@RequestBody Category category) {
        iCategoryService.saveOrUpdate(category);
        return Result.success("添加或更新成功");
    }

    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable("id") int id) {
        iCategoryService.removeById(id);
        return Result.success("删除成功");
    }


}
